<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点击小球移动</title>
    <style>
        body {
            margin: 0;
            display: grid;
            grid-template-columns: repeat(3, 1fr);
        }
        .container {
            outline: 1px solid red;
            position: relative;
        }
        .ball {
            height: 50px;
            width: 50px;
            background: blue;
            border-radius: 50%;
            position: absolute;
        }
    </style>
</head>
<body>

    <script>
        const info = prompt('请输入行列的信息，中间用,隔开'); // '3, 4'
        // 把'3, 4'变成['3', ' 4']
        const infoArr = info.split(',');
        // 行数
        const rowCount = Number(infoArr[0]);
        // 列数
        const colCount = Number(infoArr[1]);

        // 利用行数和列数改掉body和container的样式
        document.body.setAttribute('style', `grid-template-columns: repeat(${ colCount }, 1fr)`);

        // 制作每个格子
        function createContainers () {
            // 总共的个数
            const length = rowCount * colCount;
            for (let i = 0; i < length; i++) {
                const container = document.createElement('div');
                container.setAttribute('class', 'container');
                container.setAttribute('style', `height: ${ 100 / rowCount }vh`);
                document.body.appendChild(container);
                // 调用小球制作过程
                createBall(container, i === 0);
            }
        }

        // 制作小球
        function createBall (father, isFirst) {
            const ball = document.createElement('div');
            ball.setAttribute('class', 'ball');
            if (isFirst) {
                let isMouseDown = false;
                let selfX = 0;
                let selfY = 0;

                ball.addEventListener('mousedown', function (e) {
                    isMouseDown = true;
                    selfX = e.offsetX;
                    selfY = e.offsetY;
                });
                ball.addEventListener('mouseup', function () {
                    isMouseDown = false;
                });
                document.addEventListener('mousemove', function (e) {
                    if (!isMouseDown) {
                        return;
                    }
                    const xMax = window.innerWidth / colCount - 50;
                    const yMax = window.innerHeight / rowCount - 50;

                    const x = e.clientX - selfX;
                    const y = e.clientY - selfY;

                    let realX = x;
                    if (x > xMax) {
                        realX = xMax;
                    } else if (x < 0) {
                        realX = 0;
                    }

                    let realY = y;
                    if (y > yMax) {
                        realY = yMax;
                    } else if (y < 0) {
                        realY = 0;
                    }

                    const balls = document.querySelectorAll('.ball');

                    for (const ball of balls) {
                        ball.setAttribute('style', `
                            left: ${ realX }px;
                            top: ${ realY }px;
                        `);
                    }
                });
            }
            father.appendChild(ball);
        }

        createContainers();
        
    </script>
</body>
</html>